/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestTokenizers.java
 *
 * Created on Nov 26, 2009, 2:42:23 PM
 */

package update2.token;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author elisha
 */
public class TestTokenizers extends javax.swing.JFrame {

    /** Creates new form TestTokenizers */
    public TestTokenizers() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setRollover(true);

        jLabel1.setText("String:");
        jToolBar1.add(jLabel1);

        jTextField1.setText("jTextField1");
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });
        jToolBar1.add(jTextField1);

        jButton1.setText("GO!!");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);

        jScrollPane1.setViewportView(jTree1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        TokenNode topNode;
        try {
            topNode = Parser.parseTree(jTextField1.getText());
            jTree1.setModel(new DefaultTreeModel(topNode));
            jTextField1.setForeground(Color.BLACK);
        } catch (ParseException ex) {
            Color col = null;
            switch (ex.code) {
                case PARENTHETICAL_ERROR: col = Color.ORANGE; break;
                case UNRECOGNIZED_SYMBOL: col = Color.GREEN; break;
                case INVALID_OPERATOR_POSITION: col = Color.RED; break;
                case ENDED_EARLY: col = Color.GRAY; break;
                case UNKNOWN_ERROR: col = Color.MAGENTA; break;
            }
            jTextField1.setForeground(col);
            Logger.getLogger(TestTokenizers.class.getName()).log(Level.SEVERE, null, ex);
        }
            //jTextField1.repaint();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        jButton1ActionPerformed(null);
    }//GEN-LAST:event_jTextField1KeyReleased

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestTokenizers().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables

}
